package dingtalk

import (
	"parking-lot-go-dingding/model"
	"time"
)

type DingtalkBannerStruct struct {
	Id        int       `json:"id" gorm:"type:int(10) auto_increment; NOT NULL; primary_key"`
	ClassId   int       `json:"class_id" gorm:"type:int(10); NOT NULL; COMMENT:'分类ID'"`
	Title     string    `json:"title" gorm:"type:varchar(100);  COMMENT:'标题'"`
	ImagePath string    `json:"image_path" gorm:"type:varchar(100); COMMENT:'图片路径'"`
	LinkUrl   string    `json:"link_url" gorm:"type:varchar(100);  COMMENT:'链接地址'"`
	Sort      int       `json:"sort" gorm:"type:int(11); COMMENT:'排序'"`
	CreatedAt time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:datetime"`
}

func (c *DingtalkBannerStruct) TableName() string {
	return "dingtalk_banner"
}

// 新闻列表
func (c *DingtalkBannerStruct) List(classId int) ([]DingtalkBannerStruct, error) {

	var list []DingtalkBannerStruct

	//查询条件
	where := map[string]interface{}{}
	where["class_id"] = classId

	db := model.Db

	if err := db.
		Table(c.TableName()).
		Where(where).
		Order("sort desc").
		Scan(&list).Error; err != nil {
		return nil, err
	}

	return list, nil
}
